package com.szyjjk.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 功能描述： 日期处理工具类
 */
public class DateUtil {

    /**
     * 返回当前日期 yyyy/MM/dd
     *
     * @return
     */
    public static String getToDate() {
        Calendar c = Calendar.getInstance();
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONDAY) + 1;
        int d = c.get(Calendar.DATE);
        return y + "/" + m + "/" + d;
    }

    /**
     * 返回当前日期减一天
     *
     * @return
     */

    public static String getNewDateMinusOne() {
        Date dateMin = getDateMin();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dateMin);
        rightNow.add(Calendar.DAY_OF_MONTH, -1);
        Date dt1 = rightNow.getTime();
        return dateToString(dt1);
    }

    /**
     * 当前时间加7天
     *
     * @return
     */
    public static String getNewDateAddOne() {
        Date dateMin = getDateMin();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dateMin);
        rightNow.add(Calendar.DAY_OF_MONTH, 7);
        Date dt1 = rightNow.getTime();
        return dateToString(dt1);
    }
    /**
       * 当前天 增减或减少n天
     * @param n
     * @return
     */
    public static String getNewDateAddN(int n) {
        Date dateMin = getDateMin();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dateMin);
        rightNow.add(Calendar.DAY_OF_MONTH, n);
        Date dt1 = rightNow.getTime();
        return dateToString(dt1);
    }
    /**
     * 当前时间的上个月时间
     *
     * @return
     */
    public static String getLastMonth() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        
        return dateToString(date);
    }

    /**
     * @return int
     * @Description //TODO 输入日期 返回第几天
     * @Date 17:43 2019/10/16
     * @Param [date]
     **/
    public static String getnewYeayDay(Date date) {

        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        String year = ca.get(Calendar.YEAR) + "";
        int i = ca.get(Calendar.DAY_OF_YEAR);
        if (i < 10) {
            return year + "00" + i;
        } else if (i < 100) {
            return year + "0" + i;
        } else {
            return year + i + "";
        }

    }

    /**
     * 输入日期 返回第几天
     *
     * @param date
     * @return
     */
    public static int getnewYeayDay1(Date date) {

        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
//		String year = ca.get(Calendar.YEAR) + "";
        int i = ca.get(Calendar.DAY_OF_YEAR);
//		if (i < 10) {
//			return year + "00" + i;
//		} else if (i < 100) {
//			return year + "0" + i;
//		} else {
//			return year + i + "";
//		}
        return i;
    }

    /**
     * 功能描述：将今年加上天返回日期：167
     *
     * @param ，如"2015-06-06" 返回值:
     * @return Date 日期
     * @throw 异常描述
     */
    public static Date getDaytoMonth(long day, int year) {
        Calendar cal = Calendar.getInstance();
        if (year == 0) {
            year = cal.get(Calendar.YEAR);
        }
        String dateStr1 = year + "-01-01";
        // 加上的天数
        long dayMount = day - 1;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1;
        try {
            date1 = dateFormat.parse(dateStr1);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
        // 开始的日期相对于1970年1月1日的毫秒数+天数的毫秒数
        long addMill = date1.getTime() + dayMount * 24 * 3600 * 1000;
        cal.setTimeInMillis(addMill);
        String format = dateFormat.format(cal.getTime());
        return cal.getTime();
    }

    /**
     * date 转 string yyyy-MM-dd
     *
     * @param sourceDate
     * @return
     */
    public static String dateToString(Date sourceDate) {
        if (sourceDate == null) {
            return "";
        }
        SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy-MM-dd");
        return sdfNyr.format(sourceDate);
    }

    /**
     * @param sourceDate
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String dateToString2(Date sourceDate) {
        if (sourceDate == null) {
            return "";
        }
        SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdfNyr.format(sourceDate);
    }

    /**
     * @param sourceDate
     * @return yyyyMMddHHmmss
     */
    public static String dateToString3(Date sourceDate) {
        if (sourceDate == null) {
            return "";
        }
        SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdfNyr.format(sourceDate);
    }

    /**
     * date 转string yyyy/MM/dd
     *
     * @param sourceDate
     * @return
     */
    public static String dateToString1(Date sourceDate) {
        if (sourceDate == null) {
            return "";
        }
        SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy/MM/dd");
        return sdfNyr.format(sourceDate);
    }

    /**
     * 获取当前时间 如20200427120001
     *
     * @return
     */
    public static String getDate() {
        // 日期格式对象yyyy-MM-dd hh24:mi:ss
        SimpleDateFormat sdfNyrsfm = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdfNyrsfm.format(new Date());
    }

    /**
     * 功能描述：将yyyy/MM/dd格式的字符串转化为日期类型 输入参数：
     *
     * @return Date 日期
     * @throw 异常描述
     */
    public static Date stringToDate(String sourceString) {
        try {
            if (sourceString == null) {
                return new Date();
            }
            String sourceStringTrim = sourceString.trim();
            if (sourceStringTrim.length() == 0) {
                return new Date();
            }
            SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy/MM/dd");
            return sdfNyr.parse(sourceStringTrim);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * 功能描述：将yyyy-MM-dd格式的字符串转化为日期类型 输入参数：
     *
     * @return Date 日期
     * @throw 异常描述
     */
    public static Date stringToDate2(String sourceString) {
        try {
            if (sourceString == null) {
                return new Date();
            }
            String sourceStringTrim = sourceString.trim();
            if (sourceStringTrim.length() == 0) {
                return new Date();
            }
            SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy-MM-dd");
            return sdfNyr.parse(sourceStringTrim);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * @return java.util.Date
     * @Author yangyue
     * @Description //TODO String 转Date  如果是空返回null
     * @Date 9:24 2020/6/19
     * @Param [sourceString]
     **/
    public static Date stringToDate4(String sourceString) {
        try {
            if (sourceString == null || sourceString.equals("null")) {
                return null;
            }
            String sourceStringTrim = sourceString.trim();
            if (sourceStringTrim.length() == 0) {
                return null;
            }
            SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy-MM-dd");
            return sdfNyr.parse(sourceStringTrim);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }


    /**
     * @return java.util.Date[]
     * @Author yangyue
     * @Description //TODO 时间范围的转化，如果相同那么就返回0点和11点
     * @Date 9:40 2020/6/19
     * @Param [begtime, endtime]
     **/
    public static Date[] StringToDate5(String begtime, String endtime) {
        Date beginTime = DateUtil.stringToDate4(begtime);
        Date endTime = DateUtil.stringToDate4(endtime);
        //如果是一天，就取00:00 到23:59
        if (begtime != null && begtime.equals(endtime) && begtime.length() > 5) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(beginTime);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            beginTime = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            endTime = calendar.getTime();
        }
        //结果时间肯定得加一个23:59
        if (endTime != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endTime);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            endTime = calendar.getTime();
        }

        return new Date[]{beginTime, endTime};
    }


    /**
     * 功能描述：将yyyy-MM-dd HH:mm:ss格式的字符串转化为日期类型 输入参数：
     *
     * @return Date 日期
     * @throw 异常描述
     */
    public static Date stringToDate3(String sourceString) {
        try {
            if (sourceString == null) {
                return new Date();
            }
            String sourceStringTrim = sourceString.trim();
            if (sourceStringTrim.length() == 0) {
                return new Date();
            }
            SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdfNyr.parse(sourceStringTrim);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * 返回当天的最小时间
     *
     * @return
     */
    public static Date getDateMin() {

        Date date2 = new Date();
        String dateToString = DateUtil.dateToString(date2);
        dateToString = dateToString + " 00:00:00";

        try {
            SimpleDateFormat sdfNyr = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            return sdfNyr.parse(dateToString);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * 返回当天的最大时间
     *
     * @return
     */
    public static Date getDateMan() {

        Date date2 = new Date();
        String dateToString = DateUtil.dateToString(date2);
        dateToString = dateToString + " 23:59:59";

        try {
            SimpleDateFormat sdfNyr = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            return sdfNyr.parse(dateToString);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

    /**
     * 获取当前天最小时间并减去一天
     *
     * @return
     */
    public static Date getNewDateRed1() {
        Date dateMin = getDateMin();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dateMin);
        rightNow.add(Calendar.DAY_OF_MONTH, -1);
        Date dt1 = rightNow.getTime();
        return dt1;
    }

    /**
     * @param value
     * @param
     * @return
     */
    public static boolean isDate(String value) {

        SimpleDateFormat sdf = null;
        ParsePosition pos = new ParsePosition(0);// 指定从所传字符串的首位开始解析

        if (StringUtils.isBlank(value)) {
            return false;
        }
        try {
            sdf = new SimpleDateFormat("yyyy/MM/dd");
            sdf.setLenient(false);
            Date date = sdf.parse(value, pos);
            if (date == null) {
                return false;
            } else {
                // System.out.println("-------->pos : " + pos.getIndex());
                // System.out.println("-------->date : " + sdf.format(date));
                // 更为严谨的日期,如2011-03-024认为是不合法的
                if (pos.getIndex() > sdf.format(date).length()) {
                    return false;
                }
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取两个时间相隔多少天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int getDayNum(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) {
            // 同一年
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
                {
                    timeDistance += 366;
                } else {
                    // 不是闰年
                    timeDistance += 365;
                }
            }
            // System.out.println(timeDistance + (day2-day1)); ;
            return timeDistance + (day2 - day1);
        } else {
            // System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2 - day1;
        }
    }

    /**
     * 返回当前年
     *
     * @return
     */
    public static int getYear() {
        Calendar cal = Calendar.getInstance();//使用日历类
        return cal.get(Calendar.YEAR);//得到年
    }
    /**
     * 返回年的第几周
     *
     * @return
     */
    public static int getweekly(Date date) {
        Calendar calendar = Calendar.getInstance();  
        calendar.setFirstDayOfWeek(Calendar.MONDAY);  
        calendar.setTime(date);  
       return calendar.get(Calendar.WEEK_OF_YEAR);  
    }
    /**
     * 获取当前周 周一和周末的日期
     * @param date
     * @return
     */
    public static  Map<String,String> getWeekDate(Date date) {
    	 Map<String,String> map = new HashMap<>();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

         Calendar cal = Calendar.getInstance();
         cal.setTime(date);
         // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
         cal.setFirstDayOfWeek(Calendar.MONDAY);
         // 获得当前日期是一个星期的第几天
         int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
         if(dayWeek==1){
             dayWeek = 8;
         }

         cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
         Date mondayDate = cal.getTime();
         String weekBegin = sdf.format(mondayDate);

         cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
         Date sundayDate = cal.getTime();
         String weekEnd = sdf.format(sundayDate);

         map.put("mondayDate", weekBegin);
         map.put("sundayDate", weekEnd);
         return map;
    }

    /**
     * 当前时间加小时
     *
     * @param
     * @param hour
     * @return
     */
    public static Date addDateMinut(Date date, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        //System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        //return format.format(date);
        return date;
    }

    /**
     * @return java.util.Date
     * @Author yangyue
     * @Description //TODO 对时间进行增加或减少
     * @Date 12:28 2020/12/4
     * @Param [date, value, type]
     **/
    public static Date addDateValue(Date date, int value, String type) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        switch (type) {
            case "y":
                cal.add(Calendar.YEAR, value);
                break;
            case "M":
                cal.add(Calendar.MONTH, value);
                break;
            case "d":
                cal.add(Calendar.DAY_OF_YEAR, value);
                break;
            case "H":
                cal.add(Calendar.HOUR, value);
                break;
            case "":
                cal.add(Calendar.MINUTE, value);
                break;
            case "m":
                cal.add(Calendar.SECOND, value);
                break;
            case "s":
                cal.add(Calendar.YEAR, value);
                break;
        }
        return cal.getTime();

    }


    /**
     * 获取 对应年 每个月多少天
     *
     * @param
     * @return list 12 个月对应的天数
     */
    public static List<Integer> actualMaximum(Integer year) {
        List<String> strD = new ArrayList<>();
        strD.add(year + "-01-01");
        strD.add(year + "-02-01");
        strD.add(year + "-03-01");
        strD.add(year + "-04-01");
        strD.add(year + "-05-01");
        strD.add(year + "-06-01");
        strD.add(year + "-07-01");
        strD.add(year + "-08-01");
        strD.add(year + "-09-01");
        strD.add(year + "-10-01");
        strD.add(year + "-11-01");
        strD.add(year + "-12-01");
        List<Integer> strT = new ArrayList<>();
        for (String string : strD) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateUtil.stringToDate2(string));
            int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            strT.add(actualMaximum);
        }
        return strT;
    }

    /**
     * @return java.util.Date
     * @Author yangyue
     * @Description //TODO 处理潮汐数据的高度问题
     * @Date 15:36 2021/1/12
     * @Param [hours, dayTime]
     **/
    public static Date getHeightTime(String hours, Date dayTime) {
        if (hours.contains("&") || hours.contains(" ")) {
            return null;
        }
        String hour = hours.substring(0, 2);
        String minute = hours.substring(2, 4);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dayTime);
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
        calendar.set(Calendar.MINUTE, Integer.parseInt(minute));
        return calendar.getTime();
    }


   
    /**
     * @Author yangyue
     * @Description //TODO 一个时间范围转化为多个时间范围与2020的时间范围
     * @Date 10:32 2021/1/14
     * @Param [begDate, endDate]
     * @return java.util.Map<java.lang.Integer,java.util.Date[]> int 为时间， 后面为范围
     **/
    public static Map<Integer, Date[]> conversionData(Date begDate, Date endDate) {
        //2020-01-01 00:00:00
        Date beg2020 = new Date(1577808000000L);
        //2020-12-31 23:59:59
        Date end2020 = new Date(1609430399000L);
        //首先判断横跨几年
        Calendar begCalendar = Calendar.getInstance();
        begCalendar.setTime(begDate);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);

        Map<Integer, Date[]> result = new LinkedHashMap<>();
        //yearNumber 横跨几年
        int yearNumber = endCalendar.get(Calendar.YEAR) - begCalendar.get(Calendar.YEAR);
        if (yearNumber == 0) {
            //等于0 就代表没有跨一年，只需要
            begCalendar.set(Calendar.YEAR, 2020);
            endCalendar.set(Calendar.YEAR, 2020);
            Date[] only2020 = new Date[]{begCalendar.getTime(), endCalendar.getTime()};
            result.put(begCalendar.get(Calendar.YEAR), only2020);
        } else if (yearNumber >= 1) {
            //大于等于1 就代表了跨年了
            int begNumber = begCalendar.get(Calendar.YEAR);
            begCalendar.set(Calendar.YEAR, 2020);
            Date[] begArray = new Date[]{begCalendar.getTime(), end2020};
            result.put(begNumber, begArray);

            if (yearNumber > 1) {
                //补充中间的
                for (int i = 0; i < yearNumber - 1; i++) {
                    begNumber = (yearNumber - 1)+begNumber;
                    Date[] dataRang = new Date[]{beg2020, end2020};
                    result.put(begNumber, dataRang);
                }
            }
            int endNumber = endCalendar.get(Calendar.YEAR);
            endCalendar.set(Calendar.YEAR, 2020);
            Date[] endArray = new Date[]{beg2020, endCalendar.getTime()};
            result.put(endNumber, endArray);

        }
        return result;

    }

}
